//
//  UserCollectionViewController.m
//  LoveHealth
//
//  Created by lanou3g on 14-9-23.
//  Copyright (c) 2014年 lanou. All rights reserved.
//

#import "UserCollectionViewController.h"
#import "UserCollectionView.h"
#import "HealthNewsDetail.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
#import "FMDB.h"
#import "SQLHelper.h"
#import "HealthNewsDetailViewController.h"
#import "HealthKnowledgeDetailViewController.h"
#import "HealthFoodDetailViewController.h"
@interface UserCollectionViewController ()
@property (nonatomic,retain)UserCollectionView * collectionView;
@end

@implementation UserCollectionViewController
- (void)dealloc
{
    self.collectionView = nil;
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的收藏";
    self.view.backgroundColor = [UIColor whiteColor];
    _collectionView = [[UserCollectionView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_collectionView];
    _collectionView.tableView.delegate = self;
    _collectionView.tableView.dataSource = self;
    _collectionView.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _collectionView.tableView.backgroundColor = UIColorDDMenuTableViewCell;
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (self.favoriteArray.count == 0) {
        _collectionView.tableView.hidden = YES;
        _collectionView.backgroundView.hidden = NO;
    }
}


#pragma mark----------------------tableView代理方法-------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.favoriteArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }

    switch (self.favoiteType) {
        case newsType:{
        
            HealthNewsDetail * newsDetail = self.favoriteArray[indexPath.row];
            cell.textLabel.text = newsDetail.title;
        }
            break;
        case knowledgeType:{
            HealthKnowledgeDetail * knowledgeDetail = self.favoriteArray[indexPath.row];
            cell.textLabel.text = knowledgeDetail.title;
        }
            break;
        case foodType:{
            HealthFoodDetail * foodDetail = self.favoriteArray[indexPath.row];
            cell.textLabel.text = foodDetail.name;
        }
            break;
        default:
            break;
    }
    
       cell.textLabel.font = [UIFont systemFontOfSize:14.0];

    return cell;
}

#pragma mark------------------------点击进入详情-----------------------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (self.favoiteType) {
        case newsType:{
            
            HealthNewsDetail * newsDetail = self.favoriteArray[indexPath.row];
            HealthNewsDetailViewController * newsVC = [[HealthNewsDetailViewController alloc] init];

            NSLog(@"%@",newsDetail);
            newsVC.detailView.newsDetail = newsDetail;

            newsVC.favoriteDetail = newsDetail;

            [self.navigationController pushViewController:newsVC animated:YES];
            [newsVC release];
            
        }
            break;
        case knowledgeType:{
            HealthKnowledgeDetail * knowledgeDetail = self.favoriteArray[indexPath.row];
            HealthKnowledgeDetailViewController * knowledgeVC = [[HealthKnowledgeDetailViewController alloc] init];
            knowledgeVC.favoriteDetail = knowledgeDetail;
            [self.navigationController pushViewController:knowledgeVC animated:YES];
            [knowledgeVC release];
        }
            break;
        case foodType:{
            HealthFoodDetail * foodDetail = self.favoriteArray[indexPath.row];
            HealthFoodDetailViewController * foodVC = [[HealthFoodDetailViewController alloc] init];
            foodVC.favoriteDetail = foodDetail;
            [self.navigationController pushViewController:foodVC animated:YES];
            [foodVC release];
        }
            break;
        default:
            break;
    }
    
}


#pragma mark --------------------编辑tableView-------------------

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_collectionView.tableView setEditing:editing animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
//删除收藏
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (self.favoiteType) {
        case newsType:{
            
            HealthNewsDetail * newsDetail = _favoriteArray[indexPath.row];
            SQLHelper * help = [SQLHelper shareInstance];
            BOOL result = [help deleteWithID:[newsDetail.ID integerValue] tableName:KnewsTableName];

            NSLog(@"id = %ld",[newsDetail.ID integerValue]);

            if (result) {

                [_favoriteArray removeObjectAtIndex:indexPath.row];
                NSArray * deleteIndexPath = @[indexPath];
                [tableView deleteRowsAtIndexPaths:deleteIndexPath withRowAnimation:UITableViewRowAnimationLeft];
            }
            
        }
            break;
        case knowledgeType:{
            HealthKnowledgeDetail * knowledgeDetail = self.favoriteArray[indexPath.row];
            SQLHelper * help = [SQLHelper shareInstance];
            BOOL result = [help deleteWithID:[knowledgeDetail.ID integerValue] tableName:KknowledgeTableName];
            if (result) {
                [_favoriteArray removeObjectAtIndex:indexPath.row];
                NSArray * deleteIndexPath = @[indexPath];
                [tableView deleteRowsAtIndexPaths:deleteIndexPath withRowAnimation:UITableViewRowAnimationLeft];
            }
        }
            break;
        case foodType:{
            HealthFoodDetail * foodDetail = self.favoriteArray[indexPath.row];
            SQLHelper * help = [SQLHelper shareInstance];
            BOOL result = [help deleteWithID:[foodDetail.ID integerValue] tableName:KfoodTableName];
            if (result) {
                [_favoriteArray removeObjectAtIndex:indexPath.row];
                NSArray * deleteIndexPath = @[indexPath];
                [tableView deleteRowsAtIndexPaths:deleteIndexPath withRowAnimation:UITableViewRowAnimationLeft];
            }
        }
            break;
        default:
            break;
    }
}








- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
